package service

import (
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/meta"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/req"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
	"gorm.io/gorm"
)

type ServiceDictionary struct{}

var ServiceDictionaryInstance = new(ServiceDictionary)

func (service *ServiceDictionary) GetList(info req.SearchDictionaryReq) (list interface{}, total int64, err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		var tempDb = db
		if info.Name != "" {
			tempDb = tempDb.Where("name LIKE ?", "%"+info.Name+"%")
		}
		if info.Type != "" {
			tempDb = tempDb.Where("type LIKE ?", "%"+info.Type+"%")
		}
		if info.Status != nil {
			tempDb = tempDb.Where("status = ?", info.Status)
		}
		if info.SysDictionary.Desc != "" {
			tempDb = tempDb.Where("desc LIKE ?", "%"+info.SysDictionary.Desc+"%")
		}
		var tmpList []model.SysDictionary
		err = tempDb.Model(&model.SysDictionary{}).Count(&total).Error
		if err != nil {
			return
		}
		err = tempDb.Model(&model.SysDictionary{}).Offset(offset).Limit(limit).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceDictionary) DeleteList(info req.IdsReq) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		var target []model.SysDictionary
		//err = db.Where("id in ?", info.Ids).Find(&target).Error
		//少一次sql查询
		for _, id := range info.Ids {
			target = append(target, model.SysDictionary{
				RecordModel: meta.RecordModel{
					ID: uint(id),
				},
			})
		}
		err = db.Select("SysDictionaryDetails").Delete(&target).Error
		return
	})
	return
}

func (service *ServiceDictionary) Update(info model.SysDictionary) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		err = db.Save(&info).Error
		return
	})
	return
}

func (service *ServiceDictionary) Create(info *model.SysDictionary) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		err = db.Create(info).Error
		return
	})
	return
}

func (service *ServiceDictionary) DetailCreate(info *model.SysDictionaryDetail) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		err = db.Create(info).Error
		return
	})
	return
}

func (service *ServiceDictionary) DetailUpdate(info *model.SysDictionaryDetail) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		err = db.Save(info).Error
		return
	})
	return
}

func (service *ServiceDictionary) GetDetailList(info req.SearchDictionaryDetailReq) (list interface{}, total int64, err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		var tempDb = db
		tempDb = tempDb.Where("sys_dictionary_id = ?", info.DictionaryId)
		if info.Label != "" {
			tempDb = tempDb.Where("label LIKE ?", "%"+info.Label+"%")
		}
		if info.Value != 0 {
			tempDb = tempDb.Where("value = ?", info.Value)
		}
		if info.Status != nil {
			tempDb = tempDb.Where("status = ?", info.Status)
		}
		var tmpList []model.SysDictionaryDetail
		err = tempDb.Model(&model.SysDictionaryDetail{}).Count(&total).Error
		if err != nil {
			return
		}
		err = tempDb.Model(&model.SysDictionaryDetail{}).Offset(offset).Limit(limit).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceDictionary) DetailDeleteList(info req.IdsReq) (err error) {
	store.GetDataStore().OperationRecord().Execute(func(db *gorm.DB) {
		var target []model.SysDictionaryDetail
		for _, id := range info.Ids {
			target = append(target, model.SysDictionaryDetail{
				RecordModel: meta.RecordModel{
					ID: uint(id),
				},
			})
		}
		err = db.Delete(&target).Error
		return
	})
	return
}
